package com.example.controller;

import com.example.entity.DataTable;
import com.example.entity.IndicatorMetadata;
import com.example.entity.SalesData;
import com.example.mapper.SalesDataMapper;
import com.example.service.DataTableService;
import com.example.service.IndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xxx
 * @className DataCatalogController
 * @description //TODO
 * @data 2025/6/23 16:10
 */
@RestController
@RequestMapping("/api/data")
@CrossOrigin
public class DataCatalogController {
    @Autowired
    private DataTableService dataTableService;

    @Autowired
    private IndicatorService indicatorService;

    @Autowired
    private SalesDataMapper salesDataMapper;

    @GetMapping("/tables")
    public List<DataTable> getAllTables() {

        List<DataTable> allTables = dataTableService.getAllTables();
        allTables.toString();
        return allTables;
    }

    @GetMapping("/tables/{tableCode}/indicators")
    public List<IndicatorMetadata> getTableIndicators(@PathVariable String tableCode) {
        return indicatorService.getTableIndicators(tableCode);
    }

    @GetMapping("/tables/{tableCode}/sample")
    public Map<String, Object> getTableSample(@PathVariable String tableCode) {
        Map<String, Object> result = new HashMap<>();

        // 简单演示，实际应该根据tableCode动态查询
        if ("SALES".equals(tableCode)) {
            List<SalesData> sample = salesDataMapper.findSample();
            result.put("sample", sample);
            result.put("columns", Arrays.asList("产品名称", "类别", "数量", "金额", "日期", "季度", "年份"));
        } else {
            result.put("error", "暂不支持该数据表预览");
        }

        return result;
    }
}